//
//  YsNoteSDKTools.m
//  YsNoteSDK
//
//  Created by 宋宏康 on 2020/3/31.
//

#import "YsNoteSDKTools.h"
#import "YsNoteSDKHelper.h"
#import "YsNoteSDKHeader.h"
#import "YsNoteSDKDotModel.h"
#import "YsNoteSDKPageTable.h"
#import "YsNoteSDKNoteListRealmModel.h"
#import "YsNoteSDKRealmToos.h"
#import <YsToolsManager/NSString+YsToolsManagerQMUI.h>
#import <YsToolsManager/YsToolsManager.h>
#import <YYKit/YYKit.h>

@implementation YsNoteSDKTools

+ (NSDictionary <NSString *, NSString *> *)getLocalFileContentForResource:(NSString *)resource
                                                                   ofType:(NSString *)type
{
    NSParameterAssert(resource);
    NSParameterAssert(type);
    // 获取文件路径
   NSString *path = [YsNoteSDKHelper.getCurentBundle pathForResource:resource ofType:type];
   // 将文件数据化
   NSData *data = [[NSData alloc] initWithContentsOfFile:path];
   // 对数据进行JSON格式化并返回字典形式
   return [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
}

+ (NSString *)getLocalFileContentDataWithPath:(NSString *)path
{
    NSParameterAssert(path);
    NSData *data = [YsToolsManagerFileUtil readDataWithfilePath:path];
    NSString *localData =  [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    return localData;
}

+ (NSDictionary *)getDotOrderDataDicWithlocalDataArray:(NSArray <NSString *>*)localDataArray
{
    NSParameterAssert(localDataArray);
    NSMutableDictionary  <NSString *,NSMutableArray *>*_allPageDictionary = [NSMutableDictionary dictionary];
       __block int _gCurPageID =  -1;
       /// 写入到_allPageDictionary是无须的，所用用mutarray存放页面，标记下顺序
          NSMutableArray *mutArray = [NSMutableArray array];
          [localDataArray enumerateObjectsUsingBlock:^(NSString * obj, NSUInteger idx, BOOL * _Nonnull stop) {
              NSLog(@"下载的obj =======%@",obj);
              YsNoteSDKDotModel *dot =  [YsNoteSDKDotModel modelWithJSON:obj];
              if (!dot) return;
                  if (dot.PageID != _gCurPageID) {
                      _gCurPageID = dot.PageID;
                      if (![mutArray containsObject:@(_gCurPageID)]) {
                          [mutArray appendObject:@(_gCurPageID)];
                      }
                      NSString *pageString = [NSString qmui_stringWithNSInteger:dot.PageID];
                      NSMutableArray <YsNoteSDKDotModel *> *pageArr = [_allPageDictionary objectForKey:pageString];
                      if (!pageArr) {
                          pageArr = [NSMutableArray array];
                          [_allPageDictionary setObject:pageArr forKey:pageString];
                      }
                  }
                  NSMutableArray *pageArr = [_allPageDictionary objectForKey:[NSString stringWithFormat:@"%d",_gCurPageID]];
                  [pageArr appendObject:dot];
          }];
        return [_allPageDictionary copy];
}

+ (int)getPageTableLatesOrdertWithNoteID:(NSString *)noteID
{
    YsNoteSDKNoteListRealmModel *noteListModel = [YsNoteSDKRealmToos queryNoteByNote_fkcode:noteID];
    if (noteListModel.pageTableArray.count == 0) {
        return 0;
    }else{
        int orderID =  [noteListModel.pageTableArray.lastObject ys_orderiId];
        return orderID + 1;
    }
}
@end
